package com.notepad.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.notepad.R;
import com.notepad.util.SpecialCalendar;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CalendarAdapter extends BaseAdapter {
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
	private Context context;
	private String currentYear = "";
	private String currentMonth = "";
	private String currentDay = "";
	
	private Resources res = null;
	private SpecialCalendar sc = null;
	
	private String showYear = "";   //用于在头部显示的年份
	private String showMonth = "";  //用于在头部显示的月份	
	//系统时间
	private String sysData="";
	private String sys_year="";
	private String sys_month="";
	private String sys_day="";
	
	private boolean isLeapyear = false;  //是否为闰年
	private int daysOfMonth = 0;      //某月的天数
	private int dayOfWeek = 0;        //具体某一天是星期几
	private int lastDaysOfMonth = 0;  //上一个月的总天数
	private String[] dayNumber = new String[42];  //一个gridview中的日期存入此数组中
	
	private int currentFlag = -1;     //用于标记当天
	private Drawable drawable = null;
	
	public CalendarAdapter() {
		Date date = new Date();
		sysData = sdf.format(date);
		sys_year = sysData.split("-")[0];
		sys_month = sysData.split("-")[1];
		sys_day = sysData.split("-")[2];
	}
	
	public CalendarAdapter(Context context,Resources rs,int jumpMonth,int jumpYear,int cur_year,int cur_month,int cur_date){
		this();
		this.context  = context;
		this.res = rs;
		sc = new SpecialCalendar();
		
		int stepYear = cur_year + jumpYear;
		int stepMonth = cur_month + jumpMonth;
		if(stepMonth > 0){
			if(stepMonth % 12 == 0){
				stepYear = cur_year + stepMonth/12 - 1;
				stepMonth = 12;
			}else{
				stepYear = cur_year + stepMonth/12;
				stepMonth = stepMonth%12;
			}
		}else{
			//往上一个月滑动
			stepYear = cur_year - 1 + stepMonth/12;
			stepMonth = stepMonth%12 + 12;
			if(stepMonth%12 == 0){
				
			}
		}
		
		currentYear = String.valueOf(stepYear);;  //得到当前的年份
		currentMonth = String.valueOf(stepMonth);  //得到本月 （jumpMonth为滑动的次数，每滑动一次就增加一月或减一月）
		currentDay = String.valueOf(cur_date);  //得到当前日期是哪天
		getCalendar(Integer.parseInt(currentYear),Integer.parseInt(currentMonth));
		
		}
		
		public CalendarAdapter(Context context,Resources rs,int year, int month, int day){
			this();
			this.context= context;
			sc = new SpecialCalendar();
			this.res = rs;
			currentYear = String.valueOf(year);;  //得到跳转到的年份
			currentMonth = String.valueOf(month);  //得到跳转到的月份
			currentDay = String.valueOf(day);  //得到跳转到的天
			
			getCalendar(Integer.parseInt(currentYear),Integer.parseInt(currentMonth));
			
		}

	@Override
	public int getCount() {
		return dayNumber.length;
	}

	@Override
	public Object getItem(int position) {
		return dayNumber[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(R.layout.calendar_item, null);
		}
		TextView mTextView = (TextView) convertView.findViewById(R.id.calendar_item);
		String d = dayNumber[position];
		mTextView.setText(d);
		mTextView.setTextSize(18);
		mTextView.setTextColor(Color.GRAY);
		
		if (position < daysOfMonth + dayOfWeek && position >= dayOfWeek) {
			// 当前月信息显示
			mTextView.setTextColor(Color.BLACK);// 当月字体设黑
		}
		if(currentFlag == position){ 
			//设置当天的背景
			drawable = res.getDrawable(R.drawable.current_day_bgc);
			mTextView.setBackgroundDrawable(drawable);
			mTextView.setTextColor(Color.WHITE);
		}
		return convertView;
	}
	
	//得到某年的某月的天数且这月的第一天是星期几
	public void getCalendar(int year, int month){
		isLeapyear = sc.isLeapYear(year);              //是否为闰年
		daysOfMonth = sc.getDaysOfMonth(isLeapyear, month);  //某月的总天数
		dayOfWeek = sc.getWeekdayOfMonth(year, month);      //某月第一天为星期几
		lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month-1);  //上一个月的总天数
		Log.d("DAY", isLeapyear+" ======  "+daysOfMonth+"  ============  "+dayOfWeek+"  =========   "+lastDaysOfMonth);
		getweek(year,month);
	}
	
	//将一个月中的每一天的值添加入数组dayNuber中
	private void getweek(int year, int month) {
		int j = 1;

		for (int i = 0; i < dayNumber.length; i++) {

			 if(i < dayOfWeek){  //前一个月
				int temp = lastDaysOfMonth - dayOfWeek+1;
				dayNumber[i] = (temp + i)+"";
				
			}else if(i < daysOfMonth + dayOfWeek){   //本月
				String day = String.valueOf(i-dayOfWeek+1);   //得到的日期
				dayNumber[i] = i-dayOfWeek+1+"";
				//对于当前月才去标记当前日期
				if(sys_year.equals(String.valueOf(year)) && sys_month.equals(String.valueOf(month)) && sys_day.equals(day)){
					//标记当前日期
					currentFlag = i;
				}	
				setShowYear(String.valueOf(year));
				setShowMonth(String.valueOf(month));

			}else{   //下一个月
				
				dayNumber[i] = j+"";
				j++;
			}
		}
	}
	
	
	
	/**
	 * 点击每一个item时返回item中的日期
	 */
	public String getDateByClickItem(int position){
		return dayNumber[position];
	}
	
	/**
	 * 在点击gridView时，得到这个月中第一天的位置
	 */
	public int getStartPositon(){
		return dayOfWeek+7;
	}
	/**
	 * 在点击gridView时，得到这个月中最后一天的位置
	 */
	public int getEndPosition(){
		return  (dayOfWeek+daysOfMonth+7)-1;
	}
	
	public String getShowYear() {
		return showYear;
	}

	public void setShowYear(String showYear) {
		this.showYear = showYear;
	}

	public String getShowMonth() {
		return showMonth;
	}

	public void setShowMonth(String showMonth) {
		this.showMonth = showMonth;
	}
	
	
	

}
